package persist

import (
	"context"
	"gitee.com/jnshao/go-start/projects/crawler/engine"
	"log"

	"gopkg.in/olivere/elastic.v6"
)

func ItemSaver(index string) (c chan engine.Item, err error) {
	itemChan := make(chan engine.Item)

	client, err := elastic.NewClient(
		elastic.SetSniff(false)) //不使用内网链接
	if err != nil {
		return nil, err
	}
	go func() {
		itemCount := 0
		for {
			item := <-itemChan
			itemCount++
			log.Printf("Save #%vitem: %+v", itemCount, item)

			err := Save(client, index, item)
			if err != nil {
				log.Printf("Save error #%vitem: %+v", itemCount, item)
				continue
			}
		}
	}()
	return itemChan, nil
}

func Save(client *elastic.Client, index string, item engine.Item) error {
	indexService := client.Index().
		Index(index).
		Type(item.Type).
		BodyJson(item)
	if item.Id != "" {
		indexService.Id(item.Id)
	}
	_, err := indexService.Do(context.Background())
	if err != nil {
		return err
	}
	return nil
}
